package mya.mes.mdc.model.tag.util;

import mya.mes.mdc.model.excel.ExcelTagAlarmDef;
import mya.mes.mdc.model.tag.MdcConstants;

import java.util.Arrays;
import java.util.List;

public class TagHelper {


  public static ExcelTagAlarmDef[] listToArray(List<ExcelTagAlarmDef> tagAlertDefList) {
    ExcelTagAlarmDef[] arrayTmp = new ExcelTagAlarmDef[MdcConstants.ALARM_TAG_BIT_MAX];
    Arrays.fill(arrayTmp, null);
    for (ExcelTagAlarmDef def : tagAlertDefList) {
      if (def == null) {
        continue;
      }
      if (def.bit == null) {
        throw new RuntimeException("Row " + def.rowNum + ":没有定义bit");
      }
      if (def.bit < 0 && def.bit >= arrayTmp.length) {
        throw new RuntimeException("Row " + def.rowNum +
          ": bit = " + def.bit + ",定义超出范围[0," + arrayTmp.length + "]");
      }
      if (arrayTmp[def.bit] != null) {
        throw new RuntimeException("Row " + def.rowNum +
          ": bit = " + def.bit + ",出现重复定义");
      }
      arrayTmp[def.bit] = def;
    }
    return arrayTmp;
  }
}
